package com.xingkeduo.business.operation.controller;

import com.alibaba.fastjson.JSON;
import com.xingkeduo.business.operation.domain.HairstyleDomain;
import com.xingkeduo.pojo.dto.Hairstyle.HairstyleDTO;
import com.xingkeduo.pojo.dto.Hairstyle.SeeHairstyleDetailDTO;
import com.xingkeduo.pojo.json.HairstyleJson;
import com.xingkeduo.pojo.query.HairstyleQuery;
import com.xingkeduo.pojo.table.HairstyleTable;
import com.xingkeduo.tool.ResponseUtils;
import com.xingkeduo.util.pojo.exception.SystemException;
import com.xingkeduo.util.tool.ReturnUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static com.xingkeduo.tool.OperaConstants.CMS_URL_PREFIX;

/**
 * 发型库管理
 *
 * @author chenxiangda <chenxiangda@xingkeduo.com>
 * @version id:v0.1
 * @package BeautyOS
 * @copyright Copyright (c) XKD 2019
 */
@Slf4j
@RestController
@RequestMapping(CMS_URL_PREFIX +"/hairstyle")
public class HairstyleController extends InitController {

    @Autowired
    private HairstyleDomain hairstyleDomain;

//    @RequestMapping(value = "/{urlParam:.+}", method = RequestMethod.GET)
    @GetMapping(value = "/hairstyleDetail")
    public ResponseUtils doGet(@RequestParam Long id) {
        returnUtil = new ReturnUtil();
        try {
            SeeHairstyleDetailDTO seeHairstyleDetailDTO = hairstyleDomain.getHairstyleInfoById(id);
            return ResponseUtils.getSuccess(seeHairstyleDetailDTO);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return ResponseUtils.error(throwable.getMessage(), SystemException.Type.DEFAULT.toString());
        }
    }



//添加
    @RequestMapping(value = "", method = RequestMethod.POST)
    public String doPost(@RequestBody @Valid HairstyleDTO hairstyleDTO , ServerHttpRequest request) {
        returnUtil = new ReturnUtil();
        try {
            hairstyleDTO.setCreatedBy(this.getOperator(request));
            log.info("添加发型接口请求参数:{}", JSON.toJSONString(hairstyleDTO));
            int hairstyle = hairstyleDomain.createHairstyle(hairstyleDTO);
            log.info("添加发型接口返回值:{}", JSON.toJSONString(hairstyle));
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //修改
    @RequestMapping(value = "/{urlParam}", method = RequestMethod.PUT)
    public String doPut(@PathVariable String urlParam, @RequestBody @Valid HairstyleDTO hairstyleDTO , ServerHttpRequest request) {
        returnUtil = new ReturnUtil();
        try {
            //this.errMsg(bindingResult);
            HairstyleDTO hairstyle = HairstyleJson.createTableForModifyHairstyle(urlParam,hairstyleDTO);
            hairstyleDTO.setUpdatedBy(this.getOperator(request));
            log.info("修改发型接口请求参数:{}", JSON.toJSONString(hairstyleDTO));
            int i = hairstyleDomain.modifyHairstyle(hairstyle);
            log.info("修改发型接口返回参数:{}", JSON.toJSONString(i));
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //删除
    @RequestMapping(value = "/{urlParam}", method = RequestMethod.DELETE)
    public String doDelete(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            HairstyleQuery hairstyleQuery = HairstyleJson.createQueryForHairstyle(urlParam);
            long id = hairstyleQuery.getId();
            hairstyleDomain.delHairstyle(id);
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    @RequestMapping(value = "/lists", method = RequestMethod.GET)
    public String doGetLists() {
        returnUtil = new ReturnUtil();
        try {
            HairstyleQuery hairstyleQuery = new HairstyleQuery();
            List<HairstyleTable> hairstyleList= hairstyleDomain.getHairstylelists(hairstyleQuery);

            return returnUtil.getSuccess(HairstyleJson.toJson(hairstyleList));

        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }
}
